
(function(window,$,layer){
	var defaultConfig={
		formDisabled:false,
		paginationDisabled:false
	}
	var verifyConfig={
		required:function(minLength,maxLength){
			var value=$(this).val();
			return $.Deferred(function(deferred){
				if(!value){
					deferred.reject({errMsg:"不能为空"});
					return;
				}
				if(minLength && value.length<minLength){
					deferred.reject({errMsg:"字符长度过小，应大于"+minLength});
					return;
				}
				if(maxLength && value.length>maxLength){
					deferred.reject({errMsg:"字符长度过大，应小于"+maxLength});
					return;
				}
				deferred.resolve();
			}).promise();
		},
		int:function(min,max){
			var value=$(this).val();
			return $.Deferred(function(deferred){
				/*if(!value){
					deferred.reject({errMsg:"不能为空"});
					return;
				}*/
				if(!value.match(/^\d+$/) && value){
					deferred.reject({errMsg:"请输入整数"});
					return;
				}
				if(min && parseInt(value)<parseInt(min)){
					deferred.reject({errMsg:"The value is too small, the minimum value is "+min});
					return;
				}
				if(max && parseInt(value)>parseInt(max)){
					deferred.reject({errMsg:"Excessive value, maximum value "+max});
					return;
				}
				deferred.resolve();
			}).promise();
		},
		float:function(min,max){
			var value=$(this).val();
			return $.Deferred(function(deferred){
				if(!value){
					deferred.reject({errMsg:"不能为空"});
					return;
				}
				if(!$.isNumeric(value)){
					deferred.reject({errMsg:"格式错误，请输入数字"});
					return;
				}
				if(min && parseFloat(value)<parseFloat(min)){
					deferred.reject({errMsg:"数值过小，最小值为："+min});
					return;
				}
				if(max && parseFloat(value)>parseFloat(max)){
					deferred.reject({errMsg:"数值过大，最大值为："+min});
					return;
				}
				deferred.resolve();
			}).promise();
		},
		phone:function(){
			var reg=/^1\d{10}$/;
			var value=$(this).val();
			return $.Deferred(function(deferred){
				if(!value){
					deferred.reject({errMsg:"不能为空"});
					return;
				}
				if(!reg.test(value)){
					deferred.reject({errMsg:"Mobile phone number format error"});
					return;
				}
				deferred.resolve();
			}).promise();
		},
		email:function(){
			var reg=/^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/;
			var value=$(this).val();
			return $.Deferred(function(deferred){
				if(!value){
					deferred.reject({errMsg:"不能为空"});
					return;
				}
				if(!reg.test(value)){
					deferred.reject({errMsg:"Error in mailbox format"});
					return;
				}
				deferred.resolve();
			}).promise();
		}
	}
	$.extend({
		getCtx:function(){
			var js=document.scripts;
			for(var i=0;i<js.length;i++){
				var index=js[i].src.indexOf("/assets/js");
				if(index>-1){
					return js[i].src.substr(0,index);
				}
			}
		},
		toCommands:function(str){
			var res=[];
			var commands=str.split("|");
			for(var i=0;i<commands.length;i++){
				res.push(commands[i].match(/\w+/g))
			}
			return res;
		},
		verify:function(config){
			$.extend(verifyConfig,config);
		},
		initForm:function(){
			var $form=$("form.form");
			$("[data-verify]").off("blur").blur(function(){
				var $this=$(this);
				var parentDD=$this.parents("dd");
				parentDD.removeClass("invalid").removeClass("valid").addClass("pending");
				$this.verify().then(function(value){
					$this.parents("dd").removeClass("pending").addClass("valid");
				},function(error){
					$this.parents("dd").removeClass("pending").addClass("invalid");
					if(error.responseText)error=JSON.parse(error.responseText);
					var errMsg=$this.data("error-msg") || error.errMsg;
					if(errMsg)layer.tips(errMsg, $this, {tips:1,time:2000});
				});
			});
			$form.find(".btn-delete").off("click").click(function(){
				if(confirm("确定删除?")){
					location.href=location.pathname+"/delete"+location.search;
				}
			});
			if($.fn.select2&&!window.select2Inited){
				$("dd select").select2({
					minimumResultsForSearch: Infinity
				});
				window.select2Inited=true;
			}
			var submitStatus={
				valid:false,
				submited:false
			};
			$form.off("submit").submit(function(){
				
				if(submitStatus.submited){
					layer.alert("不能重复提交");
					return false;
				}
				//专门处理文本编辑器
				var html=""
				if(top.Editorhtml){
					html = top.Editorhtml.txt.html();
				}
			    //var html2=top.Editorhtml2.txt.html();
			    //var text1=top.Editorhtml.txt.text();
			    //var text2=top.Editorhtml2.txt.text();
			    if(html){
			    	$("#content").val(html);
			    }
			    /*if(html2){
			    	$("#contentEn").val(html2);
			    }
			    if(text1){
			    	$("#contentText").val(text1);
			    }
			    if(text2){
			    	$("#contentTextEn").val(text2);
			    }*/
				if(!submitStatus.valid){
					var targetInputs=$("[data-verify]").toArray();
					function next(){
						var input=targetInputs.shift();
						if(!input){
							submitStatus.valid=true;
							$form.submit();
							return;
						}
						return $(input).verify().then(function(){
							return next();
						},function(error){
							if(error.responseText)error=JSON.parse(error.responseText);
							var errMsg=$(input).data("error-msg");
							if(errMsg){
								layer.alert(errMsg);
							}else{
								$(input).focus().parents("dd").addClass("invalid");
								if(error.errMsg)layer.tips(error.errMsg, $(input), {tips:1,time:2000});
							}
						})
					}
					next();
					return false;
				}
				submitStatus.submited=true;
				return true;
			});
		}
	});
	$.fn.extend({
		verify:function(resolveCb,rejectCb){
			var resPromise=$.when();
			this.each(function(){
				var $this=$(this);
				var verify=$this.data("verify");
				var disabled=$this.attr("disabled");
				if(verify&&!disabled){
					var commands=$.toCommands(verify);
					$.each(commands,function(i,command){
						var methodName=command.shift();
						if(verifyConfig[methodName]){
							var method=verifyConfig[methodName].bind($this);
							resPromise=resPromise.then(function(){
								return method.apply($this,command);
							});
						}
					})
				}
			});
			return resPromise.promise();
		}
	});
	
	$(function(){
		var config=$.extend(defaultConfig,window.config);
		if(document.forms.length==1&&!config.formDisabled){
			$.initForm();
		}
		$(".main-right-search-submit").click(function(){
				$("input[name=page]").val(0);
				$("form[method=GET],form[method=get]").submit();
		})
		if($(".pagelist").length>0){
			$(".main-right-search i").click(function(){
				$("form[method=GET],form[method=get]").submit();
			})
			$(".pagelist .default a").click(function(){
				var page=$(this).html();
				$("[name=page]").val(page-1);
				$("form[method=GET],form[method=get]").submit();
			});
			/*$("#prevPage").off().click(function(){
				if($(this).hasClass("disabled"))return;
				var page=parseInt($("[name=page]").val())-1;
				$("[name=page]").val(page);
				$("form[method=GET],form[method=get]").submit();
			})*/
//			$("#nextPage").off().click(function(){
//				alert('b');
//				if($(this).hasClass("disabled"))return;
//				var page=parseInt($("[name=page]").val())+1;
//				$("[name=page]").val(page);
//				$("form[method=GET],form[method=get]").submit();
//			})
			$.extend({
				pageListNextPage:function(){
					if($("#nextPage").hasClass("disabled"))return;
					var page=parseInt($("[name=page]").val())+1;
					$("[name=page]").val(page);
					$("form[method=GET],form[method=get]").submit();
				}
			})
			$.extend({
				pageListPrevPage:function(){
					if($("#prevPage").hasClass("disabled"))return;
					var page=parseInt($("[name=page]").val())-1;
					$("[name=page]").val(page);
					$("form[method=GET],form[method=get]").submit();
				}
			})
		}
	});
}(window,jQuery,layer))


function previewImg(obj) {
	var img = new Image();
	img.src = obj.src;
	var imgheight = img.height;
	var height = window.screen.height;
	var imgHtml = "<img src='" + obj.src + "' height='100%' style='margin:0 auto;'/>";
	var imgwidth = img.width;
	var width = height*0.6/imgheight * imgwidth;

	//弹出层
	layer.open({
		type: 1,
		shade: 0.8,
		offset: 'auto',
		area: [width + 'px',height*0.6+'px'],  //原图显示
		shadeClose:true,
		scrollbar: false,
		title: "图片预览",
		content: imgHtml,
		cancel: function () {
		}
	});
}
